package productModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import spUtil.DB;

public class ProductEAV {
	
		int productId;
		String prodAttribute;
		String prodValue;
		
		public String getProdAttribute() {
			return prodAttribute;
		}
		public void setProdAttribute(String prodAttribute) {
			this.prodAttribute = prodAttribute;
		}
		public String getProdValue() {
			return prodValue;
		}
		public void setProdValue(String prodValue) {
			this.prodValue = prodValue;
		}
		public int getProductId() {
			return productId;
		}
		public void setProductId(int productId) {
			this.productId = productId;
		}
		
		public static ArrayList<ProductEAV> getItemDetails(int productId){	//to return details of one product
			ArrayList<ProductEAV> peavlist = new ArrayList<ProductEAV>();
			ProductEAV producteav;
			ResultSet rs = null;
			String query = "SELECT p.productId, peav.prodAttribute, peav.prodValue FROM products p, productseav peav " +
					"WHERE p.productId = peav.productId AND p.productId = " + productId;
//			System.out.println(query);
			try {
				Connection conn = DB.getConnection();
				PreparedStatement st = conn.prepareStatement(query);
				rs = st.executeQuery();
				while (rs.next()) {
					producteav = new ProductEAV();
					producteav.setProductId(rs.getInt("productId"));
					producteav.setProdAttribute(rs.getString("prodAttribute"));
					producteav.setProdValue(rs.getString("prodValue"));
					peavlist.add(producteav);
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return peavlist;
		}
		
		public static String getAttributes(ArrayList<ProductEAV> peavlist){
			String attributes = "";
			for(int i=0; i<peavlist.size(); i++){
				if(peavlist.get(i).getProdAttribute().matches("[Aa][Tt][Tt][Rr][Ii][Bb][Uu][Tt][Ee]\\d\\d*")){
					attributes = attributes.concat(peavlist.get(i).getProdValue());
					attributes = attributes.concat(";");
				}
			}
			return attributes;
		}
}
